package com.example.calendertodo.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.calendertodo.EditActivity;
import com.example.calendertodo.R;
import com.example.calendertodo.util.TodoDbOpenHelper;
import com.example.calendertodo.beam.Todo;
import com.example.calendertodo.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder>{

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<Todo> mBeanList;
    private TodoDbOpenHelper mTodoDbOpenHelper;


    public MainAdapter(Context context, List<Todo> mBeanList){
        this.mBeanList = mBeanList;
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mTodoDbOpenHelper = new TodoDbOpenHelper(mContext);
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.main_list_item, parent, false);
        MainViewHolder mainViewHolder = new MainViewHolder(view);
        return mainViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        Todo todo = mBeanList.get(position);
        String[] dates_start = todo.getStartTime().split("\t");
        String[] dates_end = todo.getEndTime().split("\t");
        String time_start = dates_start[1];
        String date_start = dates_start[0];
        String time_end = dates_end[1];
        holder.mTvTitle.setText(todo.getTitle());
        holder.mTvDate.setText(date_start);
        holder.mTvTime.setText(time_start + " - " + time_end);



        int delay = compareTime(getCurrentTimeFormat(),todo.getEndTime());
        if (delay >= 0 && todo.getState().equals("0")) { //说明逾期
            todo.setState("2");
            mTodoDbOpenHelper.updateData(todo);
        }

        //长点击事件,点击进入编辑
        holder.rlContainer.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (todo.getState().equals("0")) {
                    Intent intent = new Intent(mContext, EditActivity.class);
                    intent.putExtra("todo",todo);
                    mContext.startActivity(intent);
                }else if(todo.getState().equals("1")){
                    ToastUtil.toastShort(mContext,"Todo已完成");
                }else if(todo.getState().equals("2")){
                    ToastUtil.toastShort(mContext,"Todo逾期");
                }

                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mBeanList.size();
    }

    //将数据列表刷新
    public void refreshData(List<Todo> todo) {
        this.mBeanList = todo;
        notifyDataSetChanged();
    }

    //按位置删除，此处只是删除了recycleView中的数据还需要在数据库中删除
    public void removeData(int pos) {
        mBeanList.remove(pos);
        notifyItemRemoved(pos);
    }

    private String getCurrentTimeFormat() { //获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd\tHH:mm");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    //todo的结束时间与当前时间对比
    private int compareTime(String current_date,String end_date) {
        Log.d("tag","---------当前日期----------" + current_date + "\t截止日期" + end_date);
        return current_date.compareTo(end_date);
    }


    //列表视图的适配器
    class MainViewHolder extends RecyclerView.ViewHolder {
        TextView mTvTitle;
        TextView mTvTime;
        TextView mTvDate;
        ViewGroup rlContainer;

        public MainViewHolder(@NonNull View itemView) {
            super(itemView);
            this.mTvTitle = itemView.findViewById(R.id.tv_title_main);
            this.mTvTime = itemView.findViewById(R.id.tv_time_main);
            this.mTvDate = itemView.findViewById(R.id.tv_date_main);
            this.rlContainer = itemView.findViewById(R.id.rl_item_container_main);
        }
    }
}
